package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.ChangeMesBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Calendar;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
//修改就医卡片
public class SetPaMessage extends AppCompatActivity {

    private EditText ETChangeName;
    private RadioButton RBChangeMan;
    private RadioButton RBChangeWoman;
    private EditText ETChangeUserIDCard;
    private TextView TVChangeDate;
    private EditText ETChangePhoneNumber;
    private EditText ETChangePosition;
    private TextView btnChangeCanel;
    private TextView btnChangeOk;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pa_message);
        ETChangeName = findViewById(R.id.ET_ChangeName);
        RBChangeMan = findViewById(R.id.RB_ChangeMan);
        RBChangeWoman = findViewById(R.id.RB_ChangeWoman);
        ETChangeUserIDCard = findViewById(R.id.ET_ChangeUserIDCard);
        TVChangeDate = findViewById(R.id.TV_ChangeDate);
        ETChangePhoneNumber = findViewById(R.id.ET_ChangePhoneNumber);
        ETChangePosition = findViewById(R.id.ET_ChangePosition);
        btnChangeCanel = findViewById(R.id.btn_ChangeCanel);
        btnChangeOk = findViewById(R.id.btn_ChangeOk);
        initView();
        setBtnChangeCanel();
        setBtnChangeOk();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        Intent intent = getIntent();
        ETChangeName.setText(intent.getStringExtra("CName"));
        Log.d("TAG", "initView: "+intent.getIntExtra("CSex",0));
        if (intent.getIntExtra("CSex",0)==0){
            RBChangeWoman.setChecked(true);
        }else{
            RBChangeMan.setChecked(true);
        }
        ETChangeUserIDCard.setText(intent.getStringExtra("CCardId"));
        TVChangeDate.setText(intent.getStringExtra("CBirthday"));
        ETChangePhoneNumber.setText(intent.getStringExtra("CuserPhone"));
        ETChangePosition.setText(intent.getStringExtra("CAdd"));
        //修改出生日期
        TVChangeDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(SetPaMessage.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        TVChangeDate.setText(year+"-"+month+"-"+dayOfMonth);
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
    }
    public void setBtnChangeOk(){
        btnChangeOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/patient";
                ChangeMesBean bean = new ChangeMesBean();
                bean.setId(getIntent().getIntExtra("cardId",0));
                bean.setName(ETChangeName.getText().toString());
                bean.setCardId(ETChangeUserIDCard.getText().toString());
                bean.setTel(ETChangePhoneNumber.getText().toString());
                if (RBChangeMan.isChecked()){
                    bean.setSex(0);
                }else{
                    bean.setSex(1);
                }
                bean.setBirthday(TVChangeDate.getText().toString());
                bean.setAdders(ETChangePosition.getText().toString());
                bean.setUserId(Integer.valueOf(getSharedPreferences("selfMessage",MODE_PRIVATE).getString("userID",null)));
                Gson gson = new Gson();
                String Json  = gson.toJson(bean);
                Log.d("TAG", "setBtnChangeOk: "+Json);
                OkHttpUtils.getCall(Json,newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("TAG", "onFailure: "+e.getMessage());
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        stateBean stateBean = new Gson().fromJson(response.body().string(), com.example.smartcity.ui.Bean.stateBean.class);
                        Looper.prepare();
                        if (stateBean.getCode()==200){
                            SetPaMessage.this.finish();
                            Toast.makeText(SetPaMessage.this, "修改成功", Toast.LENGTH_SHORT).show();
                        }else{
                            SetPaMessage.this.finish();
                            Log.d("TAG", "onResponse: "+stateBean.getMsg());
                            Toast.makeText(SetPaMessage.this, "修改失败", Toast.LENGTH_SHORT).show();
                        }
                        Looper.loop();
                    }
                });
            }
        });

    }
    public void setBtnChangeCanel(){
        btnChangeCanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetPaMessage.this.finish();
            }
        });
    }
}